#!/usr/bin/env node

const fs = require('fs');
const path = require('path');

// 读取syscall.txt中的系统调用列表
function readSyscallList() {
  const syscallFile = path.resolve(__dirname, '../syscall.txt');
  const content = fs.readFileSync(syscallFile, 'utf8');
  
  // 直接读取每行作为系统调用名称（跳过空行）
  const syscalls = content
    .split('\n')
    .map(line => line.trim())
    .filter(line => line && !line.startsWith('#')); // 跳过空行和注释行
  
  return syscalls;
}

// 从SystemCallHandler.ts中读取已实现的系统调用
function readImplementedSyscalls() {
  const handlerFile = path.resolve(__dirname, '../src/syscall/SystemCallHandler.ts');
  const content = fs.readFileSync(handlerFile, 'utf8');
  
  // 查找所有以private sys开头的方法名
  const syscallRegex = /private\s+sys([A-Z][a-zA-Z0-9_]*)/g;
  const implementedSyscalls = [];
  let match;
  
  while ((match = syscallRegex.exec(content)) !== null) {
    // 将驼峰命名转换为下划线命名（如sysGetPid -> get_pid）
    const syscallName = match[1]
      .replace(/([A-Z])/g, '_$1')
      .toLowerCase()
      .substring(1); // 去掉第一个下划线
    implementedSyscalls.push(syscallName);
  }
  
  return implementedSyscalls;
}

// 找出未实现的系统调用
function findUnimplementedSyscalls() {
  const allSyscalls = readSyscallList();
  const implementedSyscalls = readImplementedSyscalls();
  
  // 创建一个Set以提高查找效率
  const implementedSet = new Set(implementedSyscalls);
  
  // 找出未实现的系统调用
  const unimplementedSyscalls = allSyscalls.filter(syscall => !implementedSet.has(syscall));
  
  return {
    all: allSyscalls,
    implemented: implementedSyscalls,
    unimplemented: unimplementedSyscalls
  };
}

// 主函数
function main() {
  try {
    const result = findUnimplementedSyscalls();
    
    console.log(`系统调用总数: ${result.all.length}`);
    console.log(`已实现的系统调用数: ${result.implemented.length}`);
    console.log(`未实现的系统调用数: ${result.unimplemented.length}`);
    
    if (result.unimplemented.length > 0) {
      console.log('\n未实现的系统调用列表:');
      result.unimplemented.forEach(syscall => console.log(`- ${syscall}`));
    } else {
      console.log('\n所有系统调用均已实现！');
    }
  } catch (error) {
    console.error('发生错误:', error);
  }
}

// 执行主函数
main();